// ========== 头文件 ==========
#include "stm32f10x.h"    
#include "freertos.h"
#include "task.h"
#include "queue.h"
#include "Delay.h"

#include <string.h>

#include "Project_Config.h"
#include "USART_Config.h"
#include "ESP01s_Control.h"
#include "HC05_Control.h"
#include "LED_SystemIndicator.h"
#include "Buzzer_Init.h"
#include "OLED.h"
#include "ADC.h"
#include "STM32_Flush.h"

// ========== 全局变量 ==========
QueueHandle_t xQueue;
#define QUEUE_LENGTH 8	 							// main 消息队列长度
#define ITEM_SIZE sizeof(USART_Message_t)			// main 消息队列项大小

// ========== 函数声明 ==========
void vTask_USART_Controller(void *arg);
void vTask_ESP_Initialize(void *arg);
void vTask_STMFlush_Initialize(void *arg);
//void vTask_ESP01s_SensorData_Push(void *arg);


int main(void){
	Delay_Init();						// 初始化 Delay函数
	
	USART_Pin_Init(&USART1_Cfg);  		// 初始化 USB_TO_TTL
	USART_Pin_Init(&USART2_Cfg);  		// 初始化 ESP01s
	USART_Pin_Init(&USART3_Cfg);  		// 初始化 HC05
	USART_NVIC_Init(&USART1_Cfg);  		// 初始化 中断源优先级
	USART_NVIC_Init(&USART2_Cfg);  		
	USART_NVIC_Init(&USART3_Cfg);  
	
	SYS_LED_Init();
	SNR_LED_Init();
	Buzzer_Init();
	OLED_Init();
	AD_Init();
	
//	OLED_Clear();
//	OLED_ShowString(1, 1, "(^v^)/ hello!");
//	OLED_ShowString(2, 1, "Connecting to hc-05   >>>");
//	HC_AutoConnect();					// 上电以后自动连接与更新OLED显示状态	

//	ESP_AutoConnect();					// 上电以后自动连接与更新OLED显示状态	
	
//	GPIO_WriteBit(SYS_LED_PORT, SYS_BLLED_PIN, (BitAction)LED_ON);
//	GPIO_WriteBit(SYS_LED_PORT, SYS_RLED_PIN, (BitAction)LED_ON);
//	GPIO_WriteBit(SNR_LED_PORT, SNR_LED_PIN, (BitAction)LED_ON);
//	while(1);

	// 在main函数测试须知：main函数这里还没有初始化接收中断的函数，所以最好在启动调度任务器以后得vtask写测试，蟹蟹！
	
	xQueue = xQueueCreate(QUEUE_LENGTH,  ITEM_SIZE);

	
	xTaskCreate(vTask_STMFlush_Initialize, "vTask_STMFlush_Initialize", configMINIMAL_STACK_SIZE * 2, NULL, 1, NULL);
	xTaskCreate(vTask_USART_Controller, "vTask_LED_Controller", configMINIMAL_STACK_SIZE * 4, NULL, 2, NULL);	
	vTaskStartScheduler();				// 启动调度器，调试到此处按F11
	
	while(1);							// 程序不应执行到这里
}

void vTask_STMFlush_Initialize(void *arg) {
    Read_Flash_Code();  // 在任务中执行ESP初始化
	
	xTaskCreate(vTask_ESP_Initialize, "vTask_ESP_Initialize", configMINIMAL_STACK_SIZE * 2, NULL, 1, NULL);
    
    vTaskDelete(NULL);  // 初始化完成后删除自身
}

void vTask_ESP_Initialize(void *arg) {
    ESP_AutoConnect();  // 在任务中执行ESP初始化
    
    vTaskDelete(NULL);  // 初始化完成后删除自身
}

void vTask_USART_Controller(void *arg) {	
	USART_Message_t msg;
	char msg_rev[MAX_STR_LEN] = {0};
	
    while(1) {		
		if(xQueueReceive(xQueue, &msg, 2000) == pdPASS){			// 写portMAX_DELAY会被持续阻塞			
			if(msg.source_id == 1){
				USART_printf(&USART1_Cfg, "USART1 Receive the str: %s \r\n", (msg_rev));	
				memset(msg_rev, 0, strlen(msg_rev));
			}
			if(msg.source_id == 2){
				// 测试语句，后期删掉1. 3. 4.
				strcpy(msg_rev, (const char*)msg.data);
				ESP_StateTransition(msg_rev); 
				USART_printf(&USART1_Cfg, "USART2 Receive the str: %s \r\n", (msg_rev));	// 注意此处输出到屏幕，而非ESP-01
//				memset(msg_rev, 0, strlen(msg_rev));						
			}
			if(msg.source_id == 3){
				OLED_ShowString(1, 1, "UR3 REVD MSG");
//				HC_StateTransition((char*)msg.data);
			}
		}
		vTaskDelay(500);
	}
}
